require('dotenv').config();
const { pool } = require('../config/database');

async function cleanGoodsTables() {
  console.log('🧹 开始清理商品相关表...');
  
  const connection = await pool.getConnection();
  try {
    // 按正确顺序删除表（先删除有外键依赖的表）
    const dropCommands = [
      'DROP TABLE IF EXISTS goods_images',
      'DROP TABLE IF EXISTS goods_entities', 
      'DROP TABLE IF EXISTS goods_property_values',
      'DROP TABLE IF EXISTS goods_properties',
      'DROP TABLE IF EXISTS goods',
      'DROP TABLE IF EXISTS goods_categories'
    ];
    
    for (const command of dropCommands) {
      try {
        await connection.execute(command);
        console.log(`✅ 执行: ${command}`);
      } catch (error) {
        console.log(`⚠️  跳过: ${command} - ${error.message}`);
      }
    }
    
    console.log('🎉 商品表清理完成！');
    
    // 查看剩余的表
    const [tables] = await connection.execute(`
      SELECT TABLE_NAME 
      FROM INFORMATION_SCHEMA.TABLES 
      WHERE TABLE_SCHEMA = DATABASE() 
      ORDER BY TABLE_NAME
    `);
    
    console.log('\n📋 当前数据库中的表:');
    tables.forEach(table => {
      console.log(`  - ${table.TABLE_NAME}`);
    });
    
  } catch (error) {
    console.error('❌ 清理失败:', error.message);
  } finally {
    connection.release();
    await pool.end();
    process.exit(0);
  }
}

cleanGoodsTables();
